package com.lam.mall.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.base.page.TableHelper;
import com.lam.common.components.dict.annotation.DictAutoTransfer;
import com.lam.common.components.easyexcel.export.DownloadUtil;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.CollectionUtil;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.mall.entity.Order;
import com.lam.mall.entity.OrderLog;
import com.lam.mall.enums.OrderStatus;
import com.lam.mall.param.DeliveryOrderParam;
import com.lam.mall.param.OrderParam;
import com.lam.mall.service.OrderItemService;
import com.lam.mall.service.OrderService;

/**
 * 订单管理Controller
 * 
 * @author admin
 * @date 2023-04-25
 */
@Log(title = "订单管理")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/mall/order")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;

    /**
     * 查询订单管理列表
     */
    @Log(oper="分页查询")
    @DictAutoTransfer
    @PreAuthorize("@ss.hasPm('mall:order:list')")
    @GetMapping("/list")
    public Result list(OrderParam param) {
    
    	if(param.getStatus() != null && param.getStatus()==0) {
    		param.setStatus(null);
    	}
        startPage();
        orderByColumn("create_time", "desc");
        List<Order> list = orderService.selectList(param);
        if(CollectionUtil.isNotEmpty(list)) {
        	list.forEach(t->{
        		t.setOrderItems(orderItemService.selectByOrderNo(t.getOrderNumber()));
        	});
        }
        return Result.ok(getTableData(list));
    }

    /**
     * 获取订单管理详细信息
     */
    @DictAutoTransfer
    @Log(oper="根据ID查询")
    @PreAuthorize("@ss.hasPm('mall:order:query')")
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable("id") String id) {
    
        return Result.ok(orderService.selectById(id));
    }

    /**
     * 发货
     */
    @PreAuthorize("@ss.hasPm('mall:order:delivery')")
    @Log(businessType = BusinessType.UPDATE, oper = "订单发货")
    @PutMapping("/delivery")
    public Result delivery(@Validated @RequestBody DeliveryOrderParam deliveryOrderParam) {
        
    	orderService.delivery(deliveryOrderParam);
        return Result.ok();
    }
    
    /**
     * 添加订单日志或关闭订单
     */
    @PreAuthorize("@ss.hasPm('mall:order:delivery')")
    @Log(businessType = BusinessType.UPDATE, oper = "订单发货")
    @PostMapping("/addLog")
    public Result addLog(@Validated @RequestBody OrderLog orderLog) {
        
    	orderService.addLog(orderLog);
        return Result.ok();
    }
    
    /**
     * 导出待发货订单
     */
    @PreAuthorize("@ss.hasPm('mall:order:exportPadyed')")
    @Log(businessType = BusinessType.EXPORT, oper = "导出待发货订单")
    @GetMapping("/exportPaid")
    public Result exportPaidOrder(OrderParam param) {
    
        param.setShopId(TokenUtils.getLoginUserShopId());
        param.setStatus(OrderStatus.PAID.value());
        TableHelper.orderByColumn("create_time", "desc");
        List<Order> list = orderService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("待发货订单", list, Order.class));
    }
    
    /**
     * 导出销售记录
     */
    @PreAuthorize("@ss.hasPm('mall:order:exportPadyed')")
    @Log(businessType = BusinessType.EXPORT, oper = "导出销售记录")
    @GetMapping("/export")
    public Result export(OrderParam param) {
    
        param.setShopId(TokenUtils.getLoginUserShopId());
        param.setStatus(OrderStatus.PAID.value());
        TableHelper.orderByColumn("create_time", "desc");
        List<Order> list = orderService.selectList(param);
        return Result.ok(DownloadUtil.exportExcel("销售记录", list, Order.class));
    }

}
